import 'package:flutter/material.dart';

/* 应用目录的list item */
/* 用到了 PopupMenuButton */
class MainItem extends StatelessWidget {
  const MainItem({
    Key key,  
    @required this.text,
    @required this.routeName,
    @required this.iconData,
    this.trailingIconData,
    this.trailingMap,
    this.isDone = false
  }):super(key:key);

  final String text; // 标题
  final String routeName; // 路由名
  final IconData iconData; // 图标
  final IconData trailingIconData; // 更多图标
  final Map<String, String> trailingMap; // 更多popMenu数据
  final bool isDone; // 是否已完成

  void navigateNamed(BuildContext context, String routeName){
    
    Navigator.pushNamed(context, routeName);
  }

  @override
  Widget build(BuildContext context) {
    if(trailingIconData != null){
        return ListTile(
          leading: Icon(iconData, color: isDone ? Colors.green : Colors.red),
          title: Text(text),
          trailing: new PopupMenuButton(
                icon: Icon(trailingIconData),
                onSelected: (value){
                  navigateNamed(context, value);
                },
                itemBuilder: (BuildContext context) {
                  // 遍历得到PopupMenu的数据项
                  List<PopupMenuEntry> list = List<PopupMenuEntry>();
                  trailingMap.forEach((key,value){
                    list.add(new PopupMenuItem(
                        value: key,
                        child: new Text(value)
                    ));
                  });
                  return list;
                },
              ),
        );
    }else{
        return ListTile(
          leading: Icon(iconData, color: isDone ? Colors.green : Colors.red),
          title: Text(text),
          onTap: () {navigateNamed(context, routeName); }
        );
    }
  }
}